package com.ljx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljx.exception.GuliException;
import com.ljx.pojo.EduChapter;
import com.ljx.pojo.EduCourse;
import com.ljx.mapper.EduCourseMapper;
import com.ljx.pojo.EduCourseDescription;
import com.ljx.pojo.EduVideo;
import com.ljx.pojo.frontVo.CourseFrontVo;
import com.ljx.pojo.frontVo.CourseWebVo;
import com.ljx.pojo.vo.CourseInfoVo;
import com.ljx.pojo.vo.CoursePublishVo;
import com.ljx.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 李加喜
 * @since 2020-09-25
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    //实现了对课程基本类型的添加  这里涉及了对多个表的信息的添加
    @Autowired
    private EduCourseDescriptionServiceImpl eduCourseDescriptionService;
    @Autowired
    private EduChapterServiceImpl eduChapterService;
    @Autowired
    private EduVideoServiceImpl eduVideoService;
    //添加课程表
    @Override
    public String saveConsurseInfo(CourseInfoVo courseInfoVo) {
        //向课程表与课程描述表信息进行添加
        //添加课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);//实现了对课程信息的添加
        if (insert==0){
            throw new GuliException(20001,"添加课程信息失败");
        }
        //获取添加后的课程id   后面查询章节的时候需要使用课程的ID进行查询
        String cid = eduCourse.getId();
        //添加课程描述信息
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //设置描述的id就是课程的id  实现了描述表与课程表1v1的关系
        courseDescription.setId(cid);
        eduCourseDescriptionService.save(courseDescription);//实现了课程描述信息的添加
        return cid;
    }
    //根据课程的Id查询出课程的信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //System.out.println("--->"+eduCourse);
        //查询课程描述表
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        return courseInfoVo;
    }
    //实现对课程信息的修改
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if (update==0){
            throw new GuliException(20001,"修改信息失败");
        }
        //对描述信息表的修改
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }
    //课程最后的确认
    @Override
    public CoursePublishVo publishId(String courseId) {
        CoursePublishVo baseMapperPublish = baseMapper.getPublish(courseId);
        return baseMapperPublish;
    }
    //删除课程
    @Override
    public void removeCourse(String courseId) {
        //删除课程的小节
        eduVideoService.removeVideoByCourseId(courseId);
        //删除章节信息
        eduChapterService.removeChapterByCourseId(courseId);
//        //删除描述的信息
        eduCourseDescriptionService.removeById(courseId);
//        //删除课程本身信息
        int i = baseMapper.deleteById(courseId);
        if (i==0){
            throw new GuliException(20001,"删除失败");
        }
    }
    //在前台分页 条件显示课程的信息
    @Override
    public Map<String, Object> getCourseInfoList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {
        //2 根据讲师id查询所讲课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空拼接
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) { //一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())) { //二级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) { //关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) { //最新
            wrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {//价格
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam,wrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();//下一页
        boolean hasPrevious = pageParam.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //map返回
        return map;
    }
    //根据课程的ID查询课程的信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
